package cn.sxt.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.sxt.logistics.pojo.BaseData;
import cn.sxt.logistics.pojo.Customer;
import cn.sxt.logistics.pojo.CustomerExample;
import cn.sxt.logistics.pojo.MsgObject;
import cn.sxt.logistics.pojo.Order;
import cn.sxt.logistics.pojo.OrderDetail;
import cn.sxt.logistics.pojo.OrderExample;
import cn.sxt.logistics.pojo.OrderExample.Criteria;
import cn.sxt.logistics.pojo.User;
import cn.sxt.logistics.pojo.UserExample;
import cn.sxt.logistics.service.BaseDataService;
import cn.sxt.logistics.service.CustomerService;
import cn.sxt.logistics.service.OrderService;
import cn.sxt.logistics.service.UserService;
import cn.sxt.logistics.util.Constant;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private UserService userService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private BaseDataService baseDataService;

	@RequestMapping("/orderPage")
	@RequiresPermissions("order:orderPage")
	public String orderPage() {

		return "orderPage";
	}

	// 编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m, Long orderId) {

		/**
		 * 给前台共享的数据 1，业务员 2，客户 3，区间管理（基础数据表） 4，付款方式（基础数据表） 5，货运方式（基础数据表） 6，取件方式（基础数据表）
		 * 7，单位（基础数据表）
		 */

		UserExample example = new UserExample();
		cn.sxt.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo(Constant.ROLE_SALESMAN);
		// 查询所有角色名称为业务员的管理员
		List<User> users = userService.selectByExample(example);
		m.addAttribute("users", users);

		CustomerExample example1 = new CustomerExample();
		List<Customer> customers = customerService.selectByExample(example1);
		m.addAttribute("customers", customers);

		// 查询所有的区间数据
		List<BaseData> intervals = baseDataService.selectBaseDatasByParentName(Constant.BASIC_COMMON_INTERVAL);
		m.addAttribute("intervals", intervals);

		// 付款方式
		List<BaseData> payments = baseDataService.selectBaseDatasByParentName(Constant.BASIC_PAYMENT_TYPE);
		m.addAttribute("payments", payments);

		// 货运方式
		List<BaseData> freights = baseDataService.selectBaseDatasByParentName(Constant.BASIC_FREIGHT_TYPE);
		m.addAttribute("freights", freights);
		// 取件方式
		List<BaseData> fetchTypes = baseDataService.selectBaseDatasByParentName(Constant.BASIC_FETCH_TYPE);
		m.addAttribute("fetchTypes", fetchTypes);
		// 单位
		List<BaseData> units = baseDataService.selectBaseDatasByParentName(Constant.BASIC_UNIT);
		m.addAttribute("units", units);

		// 修改操作
		if (orderId != null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改的回显数据
			Order order = orderService.selectByPrimaryKey(orderId);
			m.addAttribute("order", order);
		}

		return "orderEdit";
	}

	@RequiresPermissions("order:list")
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Order> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {
		// 开始分页
		PageHelper.startPage(pageNum, pageSize);

		OrderExample example = new OrderExample();

		// 关键字不为空才做条件查询
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
			criteria1.andShippingNameLike("%" + keyword + "%");

		}

		List<Order> orders = orderService.selectByExample(example);

		// 创建分页信息对象
		return new PageInfo<Order>(orders);

	}

	// 删除功能
	@RequestMapping("/insert")
	@ResponseBody
	public MsgObject insert(@RequestBody  Order order) {
		
		System.out.println("order : "+order);
		System.out.println("orderDetails : "+order.getOrderDetails());
		
		int row = orderService.insert(order);
		MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "新增数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/update")
	@ResponseBody
	public MsgObject update(@RequestBody Order order) {
		System.out.println("order :"+order);
		int row = orderService.updateByPrimaryKeySelective(order);
		MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "修改数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long orderId) {

		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");

		int row = orderService.deleteByPrimaryKey(orderId);
		if (row == 1) {
			mo = MsgObject.createMO(1, "删除数据成功!");
		}
		return mo;
	}

	/* 根据订单的id查询出对应的订单明细数据 */
	@RequestMapping("/detail")
	@ResponseBody
	public List<OrderDetail> detail(Long orderId) {

		List<OrderDetail> orderDetails = orderService.selectOrderDetailsByOrderId(orderId);

		return orderDetails;
	}

}
